<?php
/**
 * Joomla! 1.5 component jhtraq
 *
 * @version $Id: view.html.php 2010-03-09 08:28:06 svn $
 * @author JHShop
 * @package Joomla
 * @subpackage jhtraq
 * @license GNU/GPL
 *
 * Traq type system for Joomla
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the jhtraq component
 */
class JhtraqViewNewticket extends JView {
	function display($tpl = null) {
		$model =& JhtraqHelper::getModel('jhtraq');
		$project = JRequest::getVar('project');
		$this->project = $model->getProjectBySlug($project);
		$this->error = false;
		if(JRequest::getVar('task') == 'save')
		{
			$save = true;
			if(isset($_SESSION['havecaptcha']) || isset($_POST['name']))
			{
				$modelsettings =& JhtraqHelper::getModel('settings');
	   	        $settings =$modelsettings->getConfig();
                require_once(JPATH_BASE . '/components/com_jhtraq/extras/captcha/' . $settings['captcha_engine']);
                if(validateCaptcha($_POST['captcha']))
				{
					$save = true;
				}else{
					$save = false;
					$this->captcha_error = JText::_('COM_JHTRAQ_SECURITY_CODE');
				}
			}
				//die('ss');
			if($save)
			{
                $saveid = $model->saveTicket($_POST,$this->project);
				if($saveid)
				{
					$mainframe =& JFactory::getApplication();
					$mainframe->redirect("index.php?option=com_jhtraq&view=tickets&layout=view&ticket={$saveid}&project=".$project,JText::_("Ticket Added"));
					
				}else{
					$errors = $model->getError();
					$this->error = $errors;
				}
			}
		}
		$user = JFactory::getUser();
		$this->captchaHTML = "";
		if (!$user->id) {
			$model =& JhtraqHelper::getModel('settings');
	   	    $settings =$model->getConfig();
            require_once(JPATH_BASE . '/components/com_jhtraq/extras/captcha/' . $settings['captcha_engine']);
            $this->captchaHTML = genCaptchaHTML();
		}
        parent::display($tpl);
    }
	
	function buildOptions($key, $pid=0)
	{
// 		$sel = isset($_REQUEST['filter'][$key])?$_REQUEST['filter'][$key]:'';
		if($key == 'milestone' || $key == 'component' || $key == 'version')
		{
			$skey = "{$key}_id";
			$sel = isset($_POST[$key])?$_POST[$key]:@$this->ticket->$skey;
		}else{
			$sel = isset($_POST[$key])?$_POST[$key]:@$this->ticket->$key;
		}
		$db =& JFactory::getDBO();
		switch($key)
		{
			case 'status':
				$db->setQuery("SELECT * FROM #__jhtraq_ticket_status ORDER BY name ASC");	
				break;
			case 'type':
				$db->setQuery("SELECT * FROM #__jhtraq_ticket_types ORDER BY name ASC");
				break;
			case 'severity':
				$db->setQuery("SELECT * FROM #__jhtraq_severities ORDER BY name ASC");
				break;
			case 'priority':
				$db->setQuery("SELECT * FROM #__jhtraq_priorities ORDER BY name ASC");
				break;
			case 'milestone':
				$db->setQuery("SELECT id,milestone as name FROM #__jhtraq_milestones WHERE project_id='".(int)$this->project->id."' and cancelled = 0 order by id asc");
				break;
			case 'version':
				$db->setQuery("SELECT id,version as name FROM #__jhtraq_versions WHERE project_id='".(int)$this->project->id."' order by id asc");
				break;
			case 'assigned_to':
				
				$db->setQuery("Select username as name, id from #__users where id in(0," . $this->project->managers . ")");
				break;
			case 'component':
				$db->setQuery("SELECT id, name FROM #__jhtraq_components WHERE project_id='".(int)$this->project->id."' order by id asc");
			//	$db->query();
			//	print_r($db);
				break;
				
			default:
				return;
				break;
		}
		$rows = $db->loadAssocList();
		$html = '';
		
		foreach($rows as $row)
		{
			$selected = ($sel == $row['id'])?'Selected':'';
			$html .=  '<option value="'.$row['id'].'" '.$selected.'>'. $row['name'] . '</option>';
		}
		
		echo $html;
	}
}
?>